63E - Sweets Game - CodeForces Solution


bitmasks dfs and similar dp games implementation *2000

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef DEBUG
#include <time.h>
#endif

#define all(a) (a).begin(), (a).end()
#define rev(a) (a).rbegin(), (a).rend()
#define F first
#define S second
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \
  {                                                                            \
    ++recur_depth;                                                             \
    auto x_ = x;                                                               \
    --recur_depth;                                                             \
    cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":"           \
         << __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl;         \
  }
#else
#define dbg(x)
#endif

using namespace std;
using namespace __gnu_pbds;

typedef pair<int, int> ii;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> llll;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int>> vii;
typedef vector<vii> vvii;

typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pair<ll, ll>> vll;
typedef vector<vll> vvll;

typedef vector<bool> vb;

template <class type1>
using ordered_set = tree<type1, null_type, less<type1>, rb_tree_tag,
                         tree_order_statistics_node_update>;

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
                                    !is_same<T_container, string>::value,
                                    typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
  os << '{';
  string sep;
  for (const T &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e9;
const ld EPS = 1e-9;

void solve() {
  string l1, l2, l3, l4, l5;
  getline(cin, l1);
  getline(cin, l2);
  getline(cin, l3);
  getline(cin, l4);
  getline(cin, l5);

  auto rm_s = [](string &s) {
    auto it = remove(all(s), ' ');
    s.erase(it, s.end());
  };

  rm_s(l1);
  rm_s(l2);
  rm_s(l3);
  rm_s(l4);
  rm_s(l5);

  auto rc_to_id = [](int r, int c) {
    int res = c;
    if (r > 0)
      res += 3;
    if (r > 1)
      res += 4;
    if (r > 2)
      res += 5;
    if (r > 3)
      res += 4;
    return res;
  };

  auto id_to_rc = [](int idx) {
    int row = 0;
    if (idx > 2)
      row++;
    if (idx > 6)
      row++;
    if (idx > 11)
      row++;
    if (idx > 15)
      row++;
    int col = 0;
    if (row == 0)
      col = idx;
    if (row == 1)
      col = idx - 3;
    if (row == 2)
      col = idx - 3 - 4;
    if (row == 3)
      col = idx - 3 - 4 - 5;
    if (row == 4)
      col = idx - 3 - 4 - 5 - 4;

    return ii{row, col};
  };

  vi mx_col = {3, 4, 5, 4, 3};

  int msk = 0;
  for (int i = 0; i < 3; i++)
    if (l1[i] == 'O')
      msk |= (1 << i);
  for (int i = 0; i < 4; i++)
    if (l2[i] == 'O')
      msk |= (1 << (i + 3));
  for (int i = 0; i < 5; i++)
    if (l3[i] == 'O')
      msk |= (1 << (i + 7));
  for (int i = 0; i < 4; i++)
    if (l4[i] == 'O')
      msk |= (1 << (i + 12));
  for (int i = 0; i < 3; i++)
    if (l5[i] == 'O')
      msk |= (1 << (i + 16));

  vi dp((1 << 20), -1);
  function<bool(int)> f = [&](int msk) -> bool {
    assert(0 <= msk && msk < (1 << 19));
    if (msk == 0)
      return 0;

    if (dp[msk] != -1)
      return dp[msk];

    vvi grid(5, vi(5));
    for (int i = 0; i < 3; i++)
      if (msk & (1 << i))
        grid[0][i] = 1;
    for (int i = 0; i < 4; i++)
      if (msk & (1 << (i + 3)))
        grid[1][i] = 1;
    for (int i = 0; i < 5; i++)
      if (msk & (1 << (i + 7)))
        grid[2][i] = 1;
    for (int i = 0; i < 4; i++)
      if (msk & (1 << (i + 12)))
        grid[3][i] = 1;
    for (int i = 0; i < 3; i++)
      if (msk & (1 << (i + 16)))
        grid[4][i] = 1;

    // cerr << msk << "\n";
    // for (auto x : grid)
    //   cerr << "\t" << x << "\n";

    for (int i = 0; i < 19; i++) {
      auto [row, col] = id_to_rc(i);

      if (!grid[row][col])
        continue;

      int curr_msk = msk;
      for (int j = col; j < mx_col[row]; j++) {
        if (!grid[row][j])
          break;

        curr_msk ^= (1 << (i + j - col));
        assert(curr_msk < msk);
        if (!f(curr_msk)) {
          // cerr << msk << " is winning\n";
          return dp[msk] = 1;
        }
      }

      curr_msk = msk;
      int curr_col = col;
      for (int r = row; r < 5; r++, curr_col += (r > 2 ? 0 : 1)) {
        if (curr_col >= mx_col[r] || !grid[r][curr_col])
          break;
        curr_msk ^= (1 << rc_to_id(r, curr_col));
        assert(curr_msk < msk);
        if (!f(curr_msk)) {
          // cerr << msk << " is winning\n";
          return dp[msk] = 1;
        }
      }

      curr_msk = msk;
      curr_col = col;
      for (int r = row; r < 5; r++, curr_col -= (r > 2 ? 1 : 0)) {
        if (curr_col < 0 || !grid[r][curr_col])
          break;

        curr_msk ^= (1 << rc_to_id(r, curr_col));
        assert(curr_msk < msk);
        if (!f(curr_msk)) {
          // cerr << msk << " is winning\n";
          return dp[msk] = 1;
        }
      }
    }

    // cerr << msk << " is losing\n";

    return dp[msk] = 0;
  };

  if (f(msk))
    cout << "Karlsson\n";
  else
    cout << "Lillebror\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  clock_t start = clock();

  int test_cases = 1;
  // cin >> test_cases;

  while (test_cases--)
    solve();

#ifdef DEBUG
  cerr << fixed << setprecision(10)
       << "\nTime Taken: " << (double)(clock() - start) / CLOCKS_PER_SEC
       << "s\n";
#endif
  return 0;
}


Comments

Submit
0 Comments
More Questions

1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary